/**
 * Collaborative Story Development Web Application (CSDApp) Copyright Anton
 * Strack 2014
 *
 * This file is part of Collaborative Story Development Application (CSDApp).
 *
 * CSDApp is free software: you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or any later version.
 *
 * CSDApp is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * CSDApp. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package csdwa;

import java.sql.Connection;
import java.sql.Timestamp;
import java.util.Date;

/**
 * This class handles the core functional logic of the history system.
 *
 * @author Anton Strack
 */
public class TaskManager {

    private Connection conn;
    private String requestView;
    protected String requestResultMessage;

    public TaskManager(Connection conn) {
        this.conn = conn;
    }

    /**
     * This method is used to attach the recent user interaction history to the
     * request so it is available to the jsp pages
     *
     * @param request the request to add the user histories to.
     * @return the request object containing the user histories with key
     * "userHistories"
     */
    public CsdwaRequest attachUserPendingTasks(CsdwaRequest request) {
        TaskDao tdao = new TaskDao(conn);
        User user = request.getCurrentUser();
        String max = "LIMIT " + Configuration.getPendingTasksMaxCount();// convert to string
        tdao.setMarker(QueryMarker.LIMIT, max);
        tdao.addUsingQueryMarker(QueryMarker.LIMIT);
        tdao.setUseRelatedMarkers(true);
        tdao.selectTasksByStatus(user.getId(), InfoAggregator.getInstance().getStateTypeId("completed"), false);
        request.setAttribute("userPendingTasks", tdao.getBeans());
        return request;
    }

    /**
     * This method is used to track a task. It determines when to record task
     * actions and
     *
     * @param taskId
     * @param userId
     * @param actionId
     */
    public void trackTask(int taskId, int userId, String actionName) {
        TaskDao tdao = new TaskDao(conn);
        int result = tdao.selectBeanById(taskId);
        Timestamp timestamp = new Timestamp(new Date().getTime());
        boolean stateChanged = false;
        if (result > 0) {
            Task task = tdao.getBean();
            // if the current user is the intended recipient of the task, then check if the task's state should change.
            if (task.getRecipientId() == userId) {
                if (InfoAggregator.getInstance().getStateTypeId("unread") == task.getStateId()) {
                    if ("view".equalsIgnoreCase(actionName)) {

                        task.setStateId(InfoAggregator.getInstance().getStateTypeId("read"));
                        task.setStatusDate(timestamp);
                        tdao.setColumnsToUpdate("StateID, StatusDate");
                        stateChanged = true;
                    }
                } else if (InfoAggregator.getInstance().getStateTypeId("read") == task.getStateId()) {
                    if ("view".equalsIgnoreCase(actionName)) { // update the task being read date.
                        task.setStatusDate(timestamp);
                        tdao.setColumnsToUpdate("StatusDate");
                        stateChanged = true;
                    } else if ("starttask".equalsIgnoreCase(actionName))// not a real action yet
                    {
                        task.setStateId(InfoAggregator.getInstance().getStateTypeId("inprocess"));
                        task.setStatusDate(timestamp);
                        tdao.setColumnsToUpdate("StateID, StatusDate");
                        stateChanged = true;
                    } else if ("complete".equalsIgnoreCase(actionName))// not a real action yet
                    {
                        task.setStateId(InfoAggregator.getInstance().getStateTypeId("completed"));
                        task.setStatusDate(timestamp);
                        tdao.setColumnsToUpdate("StateID, StatusDate");
                        stateChanged = true;
                    }
                } else if (InfoAggregator.getInstance().getStateTypeId("starttask") == task.getStateId()) {
                    if ("complete".equalsIgnoreCase(actionName))// not a real action yet
                    {
                        task.setStateId(InfoAggregator.getInstance().getStateTypeId("completed"));
                        task.setStatusDate(timestamp);
                        tdao.setColumnsToUpdate("StateID, StatusDate");
                        stateChanged = true;
                    }
                }
            }
            if (stateChanged == true) {
                tdao.updateBean(task);
            }
        }
    }
}
